import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ts_app_dart/screen/favorite_screen.dart';
import 'package:ts_app_dart/screen/home_page.dart';
import 'package:ts_app_dart/screen/profile_screen.dart';
import 'package:ts_app_dart/screen/setting_screen.dart';
import 'package:ts_app_dart/store/index.dart';
import 'package:ts_app_dart/widget/home_will_pop.dart';

class CustomDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          UserAccountsDrawerHeader(
            accountName: Text('Your Name'),
            accountEmail: Text('your.email@example.com'),
            currentAccountPicture: CircleAvatar(
              backgroundImage:
                  NetworkImage('https://profile-avatar.csdnimg.cn/default.jpg'),
            ),
          ),
          ListTile(
            leading: Icon(Icons.home),
            title: Text('Home'),
            onTap: () {
              // Update the state of the app
              // Then close the drawer
              Navigator.pop(context);
            },
          ),
          ListTile(
            leading: Icon(Icons.settings),
            title: Text('Settings'),
            onTap: () {
              // Then close the drawer
              Navigator.pop(context);
            },
          ),
        ],
      ),
    );
  }
}

class CustomActionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      width: 50,
      margin: EdgeInsets.only(top: 2),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(40), color: Colors.white),
      child: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () {
            print('FloatingActionButton');
          },
          backgroundColor: Colors.greenAccent),
    );
  }
}

class BootomNavigation extends StatelessWidget {
  //全局状态控制器
  final globalStateController = Get.put(GlobalStateController());
  final List<Widget> bodyPageList = [
    HomeScreen(),
    FavoriteScreen(),
    ProfileScreen(),
    SettingScreen(),
  ];
  final mainState = Get.find<GlobalStateController>().state;

  @override
  Widget build(BuildContext context) {
    print(Get.arguments);
    print('Get.arguments');

    return HomeWillPop(
      child: Scaffold(
        drawer: CustomDrawer(),
        // 缓存页面数据
        body: Obx(() => IndexedStack(
              children: bodyPageList,
              index: mainState.bottomBarIndx.value,
            )),
        // body: Obx(() => bodyPageList[mainState.bottomBarIndx.value]),
        bottomNavigationBar: Obx(
          () => BottomNavigationBar(
            currentIndex: mainState.bottomBarIndx.value,
            onTap: (int index) {
              globalStateController.changeBottomBarIndex(index);
            },
            iconSize: 30.0,
            fixedColor: Colors.red,
            type: BottomNavigationBarType.fixed,
            items: [
              BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
              BottomNavigationBarItem(
                  icon: Icon(Icons.favorite_outlined), label: "收藏"),
              BottomNavigationBarItem(
                  icon: Icon(Icons.library_add_check), label: "概述"),
              BottomNavigationBarItem(icon: Icon(Icons.settings), label: "设置")
            ],
          ),
        ),
        // floatingActionButton: CustomActionButton(),
        // floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      ),
    );
  }
}
